﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace firstrpg
{
    public class alert_message
    {
        public static SpriteFont alert_1_font;
        public static SpriteFont alert_2_font;

        private string text;
        private int time_exist = 15;//in seconds
        private double begin = 0;
        private int fade = 255;
        private Color color = Color.White;

        //constructors
        alert_message()
        {
            Console.WriteLine("calling main parent");
            
        }
        alert_message(string text)
            :base()
        {
            if (text == null) return;
            this.text = text;
        }

       

        /// <summary>
        /// adds a message alert to alert_msg client variable to be displayed on the screen for 30 seconds
        /// <para>There will be a class manager that handles the drawing of this to screen in future</para>
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool message_alert(string text)
        {

            if (text == null) return false;

            alert_message amsg = new alert_message(text);



            int holder = 1;
            if (client.Alert_Message.Length >= client.numAlertMessage-1)
            {
                for (int i = 0; i < client.numAlertMessage-1; i++)
                {
                    client.Alert_Message[i] = client.Alert_Message[holder];
                    holder++;
                }
                client.Alert_Message[client.numAlertMessage-1] = amsg;
            }
            else
            {
                client.Alert_Message[client.Alert_Message.Length + 1] = amsg;
            }



            return true;
        }

        /// <summary>
        /// Does the same thing as the original function, only changes the color
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool message_alert(string text, Color c)
        {

            if (text == null) return false;

            alert_message amsg = new alert_message(text);
            amsg.color = c;

            int holder = 1;
            if (client.Alert_Message.Length >= client.numAlertMessage - 1)
            {
                for (int i = 0; i < client.numAlertMessage - 1; i++)
                {
                    client.Alert_Message[i] = client.Alert_Message[holder];
                    holder++;
                }
                client.Alert_Message[client.numAlertMessage - 1] = amsg;
            }
            else
            {
                client.Alert_Message[client.Alert_Message.Length + 1] = amsg;
            }



            return true;
        }

        /// <summary>
        /// Updates time shown for a fade effect as well as draws each message in array to the screen.
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="elapsed"></param>
        public static void draw_messages(SpriteBatch spriteBatch, GameTime elapsed)
        {
            if (spriteBatch == null) return;
            
         
            int y = 0;
            
            
            for(int ai = 0; ai < client.Alert_Message.Count(); ai++)
            {
                alert_message aa = client.Alert_Message[ai];
                if(aa == null)
                    continue;
                if (aa.begin == 0)
                    aa.begin = elapsed.TotalGameTime.TotalSeconds;
                if ((aa.begin + aa.time_exist) <= elapsed.TotalGameTime.TotalSeconds)
                {
                    client.Alert_Message[ai] = null;
                    continue;
                }
                if (elapsed.TotalGameTime.TotalSeconds >= (aa.begin + aa.time_exist) - (aa.time_exist / 3))
                    aa.fade -= 1;
                if (aa.fade <= 30)
                {
                    client.Alert_Message[ai] = null;
                    continue;
                }

                Rectangle bkg = new Rectangle(0, y, client.screenWidth, 15);
                if (ai == client.Alert_Message.Count() - 1)
                    bkg.Height = 17;
                

                spriteBatch.Draw(client.alert_say_back, bkg, new Color(4,4,4,115));
                y += 15;
                
            }
            
            y = 0;
            
            foreach (alert_message amsg in client.Alert_Message)
            {

                
                if (amsg == null)
                    continue;
      
               
                
                
                
                byte red = amsg.color.R;
                byte green = amsg.color.G;
                byte blue = amsg.color.B;
                Color NewColor = new Color(red, green, blue, amsg.fade);

                
                spriteBatch.DrawString(alert_1_font,
                    amsg.text,
                    new Vector2(5, y),
                    NewColor);//Color.White);


                y += 15;
                
        }
            

        }
    }
}
